﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using TFS.Kanban.Commons;
using TFS.Kanban.Domain;
using TFS.Kanban.Modules.KanbanBoard.Commands;
using TFS.Kanban.Modules.KanbanBoard.Interfaces;

namespace TFS.Kanban.Modules.KanbanBoard.ViewModels
{
	public class CardViewModel : ViewModelBase<CardViewModel>, ICardViewModel, ICanChangeColor
	{
		/// <summary>
		/// Initializes a new instance of the CardViewModel class.
		/// </summary>
		public CardViewModel()
		{
			this.ChangeColor = new ChangeColorCommand( this ); //how do we decouple these two?
		}

		#region Properties

		private string _Name;
		public string Name
		{
			get { return _Name; }
			set
			{
				if ( _Name != value )
				{
					_Name = value;
					OnPropertyChanged( x => x.Name );
				}
			}
		}

		public ICollection<string> PossibleColors { get; private set; }

		public ICommand ChangeColor { get; private set; }

		private string _CardColor;
		public string CardColor
		{
			get { return _CardColor; }
			set
			{
				_CardColor = value;
				OnPropertyChanged( x => x.CardColor );
			}
		}

		#endregion

		#region CRUD

		public void Load(IUserStory cardToLoad)
		{
			this.Name = cardToLoad.Name;

			LoadColors();
		}

		private void LoadColors()
		{
			// this should be sourced from somewhere more global, 
			// probably a colorprovider or something.
			this.PossibleColors = new HashSet<string> { "White", "Blue", "Red", "Purple", "Yellow" };
			this.CardColor = PossibleColors.FirstOrDefault();
		}

		#endregion

		#region Event Handling

		public void ColorChanged( string newColor )
		{
			CardColor = newColor;
		}

		#endregion
	}
}